{% extends 'backstage/root/manage_personnel/base.html' %}
{% from 'bootstrap/pagination.html' import render_pagination %}

{% block personnel_content %}
    <h4>
        Users
        <small class="text-muted">{{ pagination.total|default(0) }}</small>
    </h4>
    <div class="table-responsive">
        <table class="table table-striped table-hover table-sm">
            <thead>
            <tr class="text-center">
                <th>#</th>
                <th>{{ _("Username") }}</th>
                <th>{{ _("Realname") }}</th>
                <th>{{ _("Permission") }}</th>
                <th>{{ _("Actions") }}</th>
            </tr>
            </thead>
            {% for user in pagination.items %}
                <tr>
                    <td class="text-center">
                        {{ pagination.total - (config['USER_PER_PAGE'] * (page - 1) + loop.index) + 1 }}
                    </td>
                    <td class="text-center">{{ user.username }}</td>
                    <td class="text-center">{{ user.realname }}</td>
                    <td class="text-center">
                        {% if user.permission == 'Root' %}
                            <span class="badge badge-danger">
                                {{ user.permission }}
                            </span>
                        {% elif user.permission == 'Admin' %}
                            <span class="badge badge-warning">
                                {{ user.permission }}
                            </span>
                        {% else %}
                            <span class="badge badge-secondary">
                                {{ user.permission }}
                            </span>
                        {% endif %}
                    </td>
                    <td class="text-center">
                        {% if not user.is_root %}
                            <div class="dropdown">
                                <button class="btn btn-sm btn-info dropdown-toggle" type="button"
                                        id="dropdownMenuButton"
                                        data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <span class="oi oi-cog"></span> {{ _("Edit") }}
                                </button>
                                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                    <a class="dropdown-item"
                                       href="{{ url_for('root.edit_profile', user_id=user.id) }}">{{ _('Edit Profile') }}</a>
                                    <a class="dropdown-item"
                                       href="{{ url_for('root.change_username', user_id=user.id) }}">{{ _('Change Username') }}</a>
                                    <a class="dropdown-item"
                                       href="{{ url_for('root.change_password', user_id=user.id) }}">{{ _('Change Password') }}</a>
                                    <div class="dropdown-divider"></div>
                                    <button class="dropdown-item badge-danger" data-toggle="modal"
                                            data-target="#confirm-delete"
                                            data-href="{{ url_for('root.delete_user', user_id=user.id) }}">
                                        <span class="oi oi-trash" aria-hidden="true"></span> {{ _("Delete") }}
                                    </button>
                                </div>
                            </div>
                        {% else %}
                            <div class="tip"><span class="oi oi-ban"></div>
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
        </table>
    </div>
    <div class="page-footer">
        {{ render_pagination(pagination) }}
    </div>

    <!-- delete modal -->
    <div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="confirmModalLabel"
         aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="confirmModalLabel">{{ _("Delete Confirm") }}</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span class="oi oi-delete"></span>
                    </button>
                </div>
                <div class="modal-body">
                    <p>{{ _("Are you sure you want to delete this item?") }}</p>
                </div>
                <div class="modal-footer">
                    <form class="delete-form" method="post">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                        <button type="button" class="btn btn-default" data-dismiss="modal">
                            <span class="oi oi-action-undo"></span> {{ _("Cancel") }}
                        </button>
                        <button class="btn btn-danger btn-confirm" type="submit">
                            <span class="oi oi-trash"></span> {{ _("Delete") }}
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block scripts %}
    {{ super() }}
    {{ ckeditor.load() }}
{% endblock %}
